package com.efast.cafe.commponent.es.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.es.common.entity.CommonEsConfig;
import com.efast.cafe.commponent.es.common.service.CommonEsConfigService;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.util.StringUtil;

@Service
public class CommonEsConfigServiceImpl implements CommonEsConfigService{
	
	final static String config_redis_key="es:config:";
	
	@Qualifier("jdbcDao")
	@Autowired
	private  JdbcDao  jdbcDao;
	
	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;

	@Override
	public CommonEsConfig getConfig(String type, String code, String from) {
		ValueOperations<String, Object> valueOperations = objectRedisTemplate.opsForValue();
		String key  = config_redis_key+code+type+(StringUtil.isNotBlank(from)?"_"+from:"");
		Object configObj = valueOperations.get(key);
		if(null!=configObj){
			return (CommonEsConfig)configObj;
		}else{
			Criteria criteria = Criteria.create(CommonEsConfig.class)
				.where("type", new Object[]{type})
				.and("code", new Object[]{code});
			if(StringUtil.isNotBlank(from)){
				criteria.and("messageFrom", new Object[]{from});
			}
			List<CommonEsConfig> list = jdbcDao.queryList(criteria);
			if(null!=list && list.size()>0){
				CommonEsConfig conf = list.get(0);
				valueOperations.set(key, conf);
				return conf;
			}
		}
		return null;
	}

	
}
